import axios from 'axios'

//   ['Access-Control-Allow-Origin'] = '*';
// axios.defaults.headers.common['content-type'] = 'application/x-www-form-urlencoded';
// axios.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
// axios.defaults.baseURL = 'http://localhost:5210/'
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  if (response) {
    const { data } = response
    // 对响应数据做点什么
    if (data && data.code) {
      if (data.code === 200) {
        return response.data
      }
    } else {
      return unsuccessful(response)
    }
  }
}, function (error) {
  // 对响应错误做点什么
  return unsuccessful(error);
});

const unsuccessful = (error) => {
  console.log(error)
  throw(error)
}

const changeUrl = (url) => {
  let urls = ''
  urls = url.replace('/personalized', 'https://interface.music.163.com/weapi/personalized')
  urls = url.replace('/cloudmusic', 'http://www.hjmin.com')
  return urls
}

class Request {
  static get (urls) {
    let url = changeUrl(urls)
    return axios.get(url)
  }
  static post (urls) {
    let url = changeUrl(urls)
    return axios.post(url)
  }
}

export default Request